package com.xky.ui.module.supplierManagement.select;

import com.xky.pojo.Supplier;
import com.xky.pojo.Warehouses;
import com.xky.utils.DBUtils;
import lombok.Getter;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class SelectSupplier {

    @Getter
    private static Integer Id;

    // 标记查询是否成功
    @Getter
    private static boolean searchSuccessful = false;

    // 存储查询结果
    @Getter
    private static List<Warehouses> searchResults;

    public static List<Supplier> searchSupplierByKeyword(String keyword){
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        List<Supplier> supplierList = new ArrayList<>();

        try {
            connection = DBUtils.getConnection();
            // 使用LIKE进行模糊查询
            preparedStatement = connection.prepareStatement(
                    "SELECT * FROM suppliers WHERE supplier_code LIKE ? OR supplier_name LIKE ? OR contact_name LIKE ? OR contact_Phone LIKE ? OR contact_email LIKE ? OR address LIKE ? OR is_active LIKE ? ORDER BY supplier_id"
            );
            String searchPattern = "%" + keyword + "%";
            preparedStatement.setString(1, searchPattern);
            preparedStatement.setString(2, searchPattern);
            preparedStatement.setString(3, searchPattern);
            preparedStatement.setString(4, searchPattern);
            preparedStatement.setString(5, searchPattern);
            preparedStatement.setString(6, searchPattern);
            preparedStatement.setString(7, searchPattern);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                supplierList.add(createSupplierFromResultSet(resultSet));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtils.closeAll(connection, preparedStatement, resultSet);
        }
        return supplierList;
    }

    //查询所有的供应商信息
    public static List<Supplier> selectAll(){
        Connection connection;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        List<Supplier> supplierList = new ArrayList<>();

        connection = DBUtils.getConnection();
        try {
            preparedStatement = connection.prepareStatement("SELECT * FROM suppliers");
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                supplierList.add(createSupplierFromResultSet(resultSet));
            }
            return supplierList;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtils.closeAll(connection, preparedStatement, resultSet);
        }
    }

    public static Supplier selectWarehousesById(int supplierId) {
        Connection connection;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        connection = DBUtils.getConnection();
        try {
            preparedStatement = connection.prepareStatement("SELECT * FROM suppliers WHERE supplier_id = ?");
            preparedStatement.setInt(1, supplierId);
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()){
                return createSupplierFromResultSet(resultSet);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtils.closeAll(connection, preparedStatement, resultSet);
        }
        return null;
    }

    private static Supplier createSupplierFromResultSet(ResultSet resultSet) throws SQLException {
        Supplier supplier = new Supplier();
        supplier.setSupplierId(resultSet.getInt("supplier_id"));
        supplier.setSupplierCode(resultSet.getString("supplier_code"));
        supplier.setSupplierName(resultSet.getString("supplier_name"));
        supplier.setContactName(resultSet.getString("contact_name"));
        supplier.setContactPhone(resultSet.getString("contact_phone"));
        supplier.setContactEmail(resultSet.getString("contact_email"));
        supplier.setAddress(resultSet.getString("address"));
        supplier.setIsActive(resultSet.getBoolean("is_active"));
        
        // 获取创建时间和更新时间
        if (resultSet.getTimestamp("created_time") != null) {
            supplier.setCreatedTime(resultSet.getTimestamp("created_time").toLocalDateTime());
        }
        if (resultSet.getTimestamp("updated_time") != null) {
            supplier.setUpdatedTime(resultSet.getTimestamp("updated_time").toLocalDateTime());
        }
        
        return supplier;
    }

    /**
     * 重置查询状态
     */
    public static void resetSearchStatus() {
        searchSuccessful = false;
        searchResults = null;
        Id = null;
    }
}
